# -*- coding: utf-8 -*-
# This code written by ChanGOGOGO
"""
呼出任务页面元素
"""
import sys
import os

current_script_dir = os.path.dirname(os.path.abspath(__file__))
parent_dir = os.path.dirname(current_script_dir)
if parent_dir not in sys.path:
    sys.path.append(parent_dir)
from pages.base_page import BasePage


class CalloutTaskPage(BasePage):
    def __init__(self, page):
        super().__init__(page)
        self.create_task_window = self._create_task_window()
        self.import_phone_window = self._import_phone_window()

    def _create_task_window(self):
        """新建呼出任务窗口"""
        element = self.page.locator(
            "//div[@role='dialog' and @aria-label='新建呼出通话任务']"
        )
        return element

    def choose_scene_placeholder(self):
        """选择场景下拉框"""
        element = self.create_task_window.get_by_role("textbox", name="选择场景")
        return element

    def scene_dropbox_option(self, option):
        """选择场景下拉框可选项，多层级都可以选择"""
        element = self.page.get_by_role("menuitem", name=option)
        return element

    def task_name_input(self):
        """任务名输入框"""
        # element = self.create_task_window.locator(
        #     "//p[text()='任务名称']/following-sibling::div/input"
        # )
        element = self.create_task_window.get_by_placeholder("请输入任务名称")
        return element

    def advance_setting_button(self):
        """【高级设置】按钮"""
        # element = self.create_task_window.locator(
        #     "//section[normalize-space()='高级设置']"
        # )
        element = self.create_task_window.get_by_text("高级设置")
        return element

    def limit_business_time_switch(self):
        """限制营业时段开关"""
        element = self.create_task_window.locator(
            "//p[text()='限制营业时段']/following-sibling::div/span"
        )
        return element

    def create_task_window_button(self, button_name="创建并导入号码"):
        """创建外呼任务窗口的按钮"""
        element = self.create_task_window.locator(
            f"//button/span[normalize-space()='{button_name}']"
        )
        return element

    def _import_phone_window(self):
        """导入号码窗口"""
        element = self.page.locator("//div[@role='dialog' and @aria-label='导入号码']")
        return element

    def import_phone_tab(self, tab_name="手动输入"):
        """导入号码窗口tab页"""
        if tab_name == "手动输入":
            xpath = f"//div[@role='tablist']//div[@role='tab' and normalize-space()='手动输入']"
        else:
            xpath = f"//div[@role='tablist']//div[@role='tab']/span[normalize-space()='{tab_name}']"
        element = self.import_phone_window.locator(xpath)
        return element

    def customer_source_input(self):
        """客户来源输入框"""
        element = self.import_phone_window.locator(
            "//label[normalize-space()='客户来源']/following-sibling::div//input"
        )
        return element

    def case_id_input(self):
        """caseID输入框"""
        element = self.import_phone_window.locator(
            "//label[normalize-space()='caseID']/following-sibling::div//input"
        )
        return element

    def phone_input(self):
        """电话号码输入框"""
        element = self.import_phone_window.locator(
            "//label[normalize-space()='电话号码']/following-sibling::div//input"
        )
        return element

    def import_phone_window_button(self, button_name="导入并关闭"):
        """导入号码窗口的按钮"""
        element = self.import_phone_window.locator(
            f"//button[normalize-space()='{button_name}']"
        )
        return element

    def start_task_button(self, task_name):
        """找到以task_name开头的任务名"""
        xpath = (
            f"//div[@class='el-table__body-wrapper is-scrolling-none']//table//tr[@class='el-table__row']"
            f"//span[starts-with(text(), '{task_name}')]/parent::div/parent::td/following-sibling::td"
            f"//button//*[@*='#icon-duoxuanxiang-bofang']"
        )
        element = self.page.locator(xpath)
        if element.count() == 1:
            return element
        else:
            return element.nth(0)


if __name__ == "__main__":
    from playwright.sync_api import Playwright, sync_playwright, expect

    def run(playwright: Playwright) -> None:
        browser = playwright.chromium.launch(headless=False)
        context = browser.new_context()
        page = context.new_page()
        page.goto("http://172.18.160.155:40080/#/user/login")
        page.get_by_placeholder("AIForce ID").fill("yaocheng1101@wezhuiyi.com")
        page.get_by_placeholder("密码").fill("admin@123")
        page.get_by_placeholder("密码").press("Enter")
        with page.expect_popup() as page1_info:
            page.get_by_role("button", name="弘毅").click()
        page1 = page1_info.value
        page1.get_by_text("呼出任务").click()
        page1.get_by_role("button", name="新建任务").click()
        callout_task_page = CalloutTaskPage(page1)
        callout_task_page.choose_scene_placeholder().click()
        callout_task_page.scene_dropbox_option("实验组").click()
        callout_task_page.scene_dropbox_option("CAPS").click()
        callout_task_page.scene_dropbox_option("CAPS-A").click()
        page1.wait_for_timeout(1000)
        callout_task_page.advance_setting_button().click()
        callout_task_page.limit_business_time_switch().click()
        callout_task_page.create_task_window_button("创建").click()
        # ---------------------
        context.close()
        browser.close()

    with sync_playwright() as playwright:
        run(playwright)
